<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue的watch监听选项</title>
</head>
<body>
  <div id="app">
    <h2>默认监听</h2>
    <h2>{{info.name}}</h2>
    <h2>---------------------------------------------------------</h2>
    <h2>深度监听</h2>
    <h2>{{info2.name}}</h2>
    <button @click="changeInfo()">修改message</button>
  </div>
  <script src="../lib/vue.js"></script>
  <script>
    // 使用vue
    const app = Vue.createApp({
      template: ``
      ,
      data: function(){
        return {
          message: "hello watch",
          info: {name: "小米info", price: 3000},
          info2: {name: "小米info2", price: 3000}
        }
      }
      ,methods: {
        changeInfo() {
          // 1. 创建新对象赋值给info
          this.info = {name: "华为info", price: 3800},
          // 2. 直接修改原对象的属性
          this.info2.name = "华为info2"
        }
      },
      watch: {
        // 默认不会进行深度监听,只会监听info,而不会监听info里面的属性是否发生变化,也就是不会深度监听
        info(newValue, oldValue) {
          console.log("默认监听: info数据发生了变化: " + "原数据: ", oldValue, "新数据: ", newValue)
        },
        info2: {
          handler(newValue, oldValue){
            // 只是改变了对象属性,并没有生成新对象,所以输出的代理对象: newValue和oldValue是一样的
            console.log("开启深度监听: info数据发生了变化: " + "原数据: ", oldValue, "新数据: ", newValue)
            console.log("生成的对象是否和原对象一样: ", newValue === oldValue)
          },
          // 对info进行深度监听
          deep: true,
          // 第一次渲染时直接执行一次监听器
          immediate: true
        },
        // 监听对象里面的某个属性
        "info.name": function(newValue, oldValue) {
         console.log("单独监听对象的某个属性: name发生了变化", newValue, oldValue)
        }
      }
    }
    )
    // 挂载 
    app.mount("#app")
  </script>

</body>
</html>
